package com.example.words;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;


/**
 * A simple {@link Fragment} subclass.
 */
public class AddFragment extends Fragment {
    private Button buttonSubmit;
    private EditText editTextEnglish,editTextChinse;
    private WordViewModel wordViewModel;

    public AddFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_add, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FragmentActivity activity = requireActivity();
        wordViewModel = new ViewModelProvider(requireActivity()).get(WordViewModel.class);
        buttonSubmit = activity.findViewById(R.id.buttonSubmit);
        editTextEnglish = activity.findViewById(R.id.editTextEnglish);
        editTextChinse = activity.findViewById(R.id.editTextChinese);

        buttonSubmit.setEnabled(false);

        //获取焦点
        editTextEnglish.requestFocus();
        //获得软键盘的实例
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        //显示软键盘
        imm.showSoftInput(editTextEnglish,0);

        //显示软键盘时解决自动收缩页面在manifest中添加如下代码
        //   <activity android:name="com.example.words.MainActivity"
        //    android:windowSoftInputMode="adjustNothing">

        //字符串变化的监听器
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //english和chinese同时不为空时，才能提交

                String english = editTextEnglish.getText().toString().trim();
                String chinese = editTextChinse.getText().toString().trim();
                buttonSubmit.setEnabled(!english.isEmpty() && !chinese.isEmpty());

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        };
        //监听字符串
        editTextEnglish.addTextChangedListener(textWatcher);
        editTextChinse.addTextChangedListener(textWatcher);

        //提交新增的单词
        buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String english = editTextEnglish.getText().toString().trim();
                String chinese = editTextChinse.getText().toString().trim();
                Word word = new Word(english,chinese);
                wordViewModel.insertWords(word);

                NavController navController = Navigation.findNavController(view);
                navController.navigateUp();

                //收起软键盘
                //获得软键盘的实例
                InputMethodManager imm = (InputMethodManager) requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                //显示软键盘
                imm.hideSoftInputFromWindow(view.getWindowToken(),0);

            }
        });
    }
}
